#!/bin/bash

#   ProxmoxVE打开IOMMU进行PCI直通

#  编辑grub
nano /etc/default/grub

#   修改内容如下

#intel_cpu
GRUB_CMDLINE_LINUX_DEFAULT="quiet intel_iommu=on"
#amd_cpu
GRUB_CMDLINE_LINUX_DEFAULT="quiet amd_iommu=on"
#   实际使用中如下：
GRUB_CMDLINE_LINUX_DEFAULT="quiet intel_iommu=on iommu=pt video=efifb:off "

#   更新grub
update-grub

# 如果启动方式不同（如果采用zfs安装系统），那就需要配置 /etc/kernel/cmdline
# Intel
quiet intel_iommu=on

# AMD
quiet amd_iommu=on

然后更新
proxmox-boot-tool refresh

更新modules
在 /etc/modules 里面添加如下内容

vfio
vfio_iommu_type1
vfio_pci
vfio_virqfd
6 接着添加模块（驱动）黑名单，即让GPU设备在下次系统启动之后不使用这些驱动，把设备腾出来给vfio驱动用：
Intel核显：

echo "blacklist snd_hda_intel" >> /etc/modprobe.d/pve-blacklist.conf
echo "blacklist snd_hda_codec_hdmi" >> /etc/modprobe.d/pve-blacklist.conf
echo "blacklist i915" >> /etc/modprobe.d/pve-blacklist.conf
N卡/A卡：

echo "blacklist nouveau" >> /etc/modprobe.d/pve-blacklist.conf
echo "blacklist radeon" >> /etc/modprobe.d/pve-blacklist.conf
更新nodules
update-initramfs -k all -u

重启系统
reboot

#   qm importdisk 102 /var/lib/vz/template/iso/DS918_7.X.img local-lvm

#   直通硬盘
#   进入PVE的SSH,或者直接进入PVE管理网页Shell
#   查看你现在的存储设备的序列号:

#   ls -l /dev/disk/by-id/
#   然后就能看到所有存储设备的序列号

#     32t usb 硬盘
#     usb-Flash_USB_Disk_3727024C3EB8061121827-0:0
#     4t 机械硬盘
#     ata-ST4000NM0053_Z1Z4AV26
#     120g 固态硬盘
#     ata-KINGSTON_SV300S37A120G_50026B77028WM019

qm set 108 -sata0 /dev/disk/by-id/ata-ST4000NM0053_Z1Z4AV26

qm set 103 -sata1 /dev/disk/by-id/ata-KINGSTON_SV300S37A120G_50026B77028WM019

# img 文件导入 存储位置 local-lvm
qm importdisk 102 ./ubuntu-20-4-04.img  local-lvm
dd if=/dev/sda of=/tmp/img/ubuntu-20-04-4.img
/root/nas/ubuntu-20-4-04.img
qm importdisk 109 /root/nas/ubuntu-20-4-04.img local-lvm


wget -o ubuntu-22-04-img.tar.gz https://gitcode.net/linux/ubuntu/-/raw/img/ubuntu-22-04-img.tar.gz?inline=false | tar -xvf - ./

wget https://gitcode.net/linux/ubuntu/-/raw/img/ubuntu-22-04-img.tar.gz

qm importdisk 102 ubuntu-22-04.img local-lvm


qm importdisk 109 /var/lib/vz/template/iso/u-nas.img local-lvm
qm importdisk 101 /mnt/date/工具/ubuntu-20-04-4.img local-lvm

/mnt/nas/工具/ubuntu-20-04-4.img

mount /dev/mapper/hhd-date /mnt/date

apt-get update
# sshfs 服务安装
apt-get install -y sshfs
# 挂载sftp
sshfs root@192.168.2.252:/mnt/HHD/date/linux/iso /root/nas/iso






 lvreduce -L -32G /dev/pve/root -f

 resize2fs /dev/mapper/pve-root

 qm importdisk 101 /root/arpl.img local-lvm

qm list #列出所有虚机
创建一个虚拟机
qm create --memory 8192 --sockets 1 --cores 2 182
启动虚拟机
qm start 109
停止虚拟机
qm stop 109
查看虚拟机状态
qm status 180
解锁虚拟机
qm unlock 180

qm listsnapshot 109  #查看虚拟机快照列表

qm rollback 109 root_init #回滚虚拟机到指定快照

qm config 109 #查看虚拟机配置信息
qm destroy 109 #删除虚拟机

14、创建一个虚拟机快照
root@s129:~# qm snapshot 180 init3 -description snapshot-test
snapshotting ‘drive-scsi0’ (local-lvm:vm-180-disk-0)
Logical volume “snap_vm-180-disk-0_init3” created.
root@s129:~# qm listsnapshot 180
Wide character in printf at /usr/share/perl5/PVE/GuestHelpers.pm line 160.
`-> init0 2023-01-03 16:25:08 centos8初始化安装完成
Wide character in printf at /usr/share/perl5/PVE/GuestHelpers.pm line 160.
`-> init1 2023-04-12 16:21:39 系统升级为centos8-stream稳定版
`-> init2 2023-04-14 16:29:14 no-description
`-> init3 2023-04-14 16:34:49 snapshot-test
`-> current You are here!

15、删除一个快照
root@s129:~# qm delsnapshot 180 init2
Logical volume “snap_vm-180-disk-0_init2” successfully removed
root@s129:~# qm listsnapshot 180
Wide character in printf at /usr/share/perl5/PVE/GuestHelpers.pm line 160.
`-> init0 2023-01-03 16:25:08 centos8初始化安装完成
Wide character in printf at /usr/share/perl5/PVE/GuestHelpers.pm line 160.
`-> init1 2023-04-12 16:21:39 系统升级为centos8-stream稳定版
`-> init3 2023-04-14 16:34:49 snapshot-test
`-> current You are here!

16、克隆一个虚拟机
root@s129:~# qm clone 180 182
create full clone of drive scsi0 (local-lvm:vm-180-disk-0)
Logical volume “vm-182-disk-0” created.
drive mirror is starting for drive-scsi0
drive-scsi0: transferred 0.0 B of 60.0 GiB (0.00%) in 0s
…
drive-scsi0: transferred 60.0 GiB of 60.0 GiB (100.00%) in 7m 23s, ready
all ‘mirror’ jobs are ready
suspend vm
trying to acquire lock…
drive-scsi0: Cancelling block job
drive-scsi0: Done.
resume vm
trying to acquire lock…

16、迁移虚拟机到其他节点
??我们先查看集群其他节点的名称，然后执行迁移命令，如果存在本地主机配置，如本地CDDROM配置，会报错“can’t migrate local disk ‘local:iso/CentOS-8-x86_64-1905-dvd1.iso’: local cdrom image”,我们可以先编辑配置文件，将本地iso配置挂载取消，然后再执行迁移命令即可。

root@s129:~# pvecm nodes

Membership information
----------------------
Nodeid Votes Name
1 1 s129 (local)
2 1 s130
3 1 s128
root@s129:~# qm migrate 182 s128

17、修改虚拟机参数
root@s129:~# qm set 180 -cores 1
update VM 150: -cores 1
root@s129:~# qm set 180 -memory 8192
update VM 181: -memory 8192

18、进入虚拟机监视器
qm monitor 180

 nano /etc/default/grub